﻿
//QString Get::osName()
//{
//#if defined(Q_OS_ANDROID)
//    return QLatin1String("android");
//#elif defined(Q_OS_BLACKBERRY)
//    return QLatin1String("blackberry");
//#elif defined(Q_OS_IOS)
//    return QLatin1String("ios");
//#elif defined(Q_OS_MAC)
//    return QLatin1String("osx");
//#elif defined(Q_OS_WINCE)
//    return QLatin1String("wince");
//#elif defined(Q_OS_WIN)
//    return QLatin1String("windows");
//#elif defined(Q_OS_LINUX)
//    return QLatin1String("linux");
//#elif defined(Q_OS_UNIX)
//    return QLatin1String("unix");
//#else
//    return QLatin1String("unknown");
//#endif
//}

//show GCC defines on Windows :
//gcc - dM - E - < NUL :
//
//    on Linux :
//gcc - dM - E - < / dev / null
//
// Windows 的 MinGW 预定义了：
// WIN32 _WIN32 __WIN32 __WIN32__ __MINGW32__ WINNT __WINNT __WINNT__ _X86_ i386 __i386

#if defined(_MSC_VER) /*&& defined(_WIN32) && defined(WIN32)*/
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <WinSock2.h>

#pragma comment(lib, "ws2_32.lib")
#elif defined(unix) || defined(__unix) || defined(__unix__)
//#warning You are using UNIX
#elif defined(__APPLE__) || defined(__MACH__)
//#warning You are using Mac OS X
#elif defined(__linux__)
//#warning You are using Linux
#elif defined(__FreeBSD__)
//#warning You are using FreeBSD
#else
#error Unknown OS
#endif


#include <vector>
#include <queue>
#include <assert.h>

//typedef std::vector<unsigned char> MyByteStream;
//typedef std::queue<unsigned char> MyByteStream;
typedef std::deque<unsigned char> MyByteStream;
std::deque<unsigned char> aa;

int EscapeWrite(MyByteStream& s, unsigned char chData)
{
    if (chData == 0xff) {
        s.push_back(0xfe);
        s.push_back(0x01);
    }
    else if (chData == 0xfe) {
        s.push_back(0xfe);
        s.push_back(0x00);
    }
    else
        s.push_back(chData);
    
    return 0;
}


void UnescapeRead(MyByteStream& s, unsigned char& chData)
{
    static unsigned char ch[2];  
    static int i = 0;// i = 0 时，不使用 ch 变量，否则使用

    if (i > 0) {
        chData = ch[i];
        i = 0;
        return;
    }

    while (!s.empty()) {

        ch[i] = s.front();
        s.pop_front();    // 相当于 ReadFile
        
        if (ch[i] == 0xfe) {
        
            ch[++i] = s.front();
            s.pop_front();    // 相当于 ReadFile

            if (ch[i] == 0x01) {
                chData = 0xff;
                i = 0;
            }
            else if (ch[i] == 0x00) {
                chData = 0xfe;
                i = 0;
            }
            else {
                // 这个应该时异常
                assert((ch[i] == 0x00) || (ch[i] == 0x01));
            }
            break;
        }
        else
        {
            assert(i == 0);
            chData = ch[i];
            break;
        }
    }
}

int main(int argc, const char* argv[])
{

    unsigned char* p = NULL;
    MyByteStream mbs;
    mbs.clear();
    //mbs.push_back('a');
    //p = mbs.data();
    //mbs.push_back('b');
    //p = mbs.data();
    //mbs.push_back('c');
    //p = mbs.data();
    //mbs.push_back('d');
    //p = mbs.data();
    //mbs.pop_back();
    //p = mbs.data();
    MyByteStream mbsOut;

    unsigned char ctlFrame[] = { 0xff, 0xff,  0x81, 0xef, 0xfe, 0xff,    0x00, 0xff };

    for (size_t i = 0; i < sizeof(ctlFrame); i++)
    {
        EscapeWrite(mbs, ctlFrame[i]);
    }

    int cnt = 0;
    while (!mbs.empty())
    {
        unsigned char ch;
        UnescapeRead(mbs, ch);
        mbsOut.push_back(ch);
        cnt++;
    }

	WORD wVer = MAKEWORD(2, 2);
	WSADATA data;
	WSAStartup(wVer, &data);

    

	WSACleanup();
	return 0;
}